package cn.fyl.system.service.impl;

import cn.fyl.basic.service.impl.BaseServiceImpl;
import cn.fyl.system.domain.Menu;
import cn.fyl.system.domain.SystemType;
import cn.fyl.system.mapper.MenuMapper;
import cn.fyl.system.service.IMenuService;
import cn.fyl.system.service.ISystemTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.text.StyledEditorKit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;



    @Override
    public List<Menu> menuTree() {
        List<Menu> menus = menuMapper.loadAll();

        HashMap<Long, Menu> map = new HashMap<>();

        for (Menu menu : menus) {
            map.put(menu.getId(),menu);
        }

        ArrayList<Menu> menuTree = new ArrayList<>();
        for (Menu menu : menus) {
            if(menu.getParent_id()==null){//顶级菜单
                menuTree.add(menu);
            }else {
                Long parent_id = menu.getParent_id();
                Menu parentMenu = map.get(parent_id);
                parentMenu.getChildren().add(menu);

            }

        }
        return menuTree;
    }
}
